import {
  createNamespacedHelpers,
  mapActions as mapRootActions,
  ActionMethod,
} from "vuex";
import { useStore } from "@/store";

const useActions = <T extends string>(args: T[], type = "") => {
  const store = useStore();

  let mapFun = mapRootActions;
  if (type) {
    const { mapActions } = createNamespacedHelpers(type);
    mapFun = mapActions;
  }

  const MapFuns = mapFun(args);
  const DealFuns: {
    [k in T]: ActionMethod;
  } = {} as {
    [k in T]: ActionMethod;
  };
  Object.keys(MapFuns).map((item) => {
    DealFuns[item as keyof typeof DealFuns] = MapFuns[
      item as keyof typeof MapFuns
    ].bind({
      $store: store,
    });
  });

  return DealFuns;
};

export { useActions };
